const axios = require('axios')

module.exports = {
  add: (req, res) => {
    const info = {
      ...req.body,
      pub_data: +new Date(),
      author_id: req.user.id,
    }
    const sql = `insert into ev_record set ?`
    db.query(sql, info, (err, rel) => {
      if (err) return res.cc(err)
      if (rel.affectedRows !== 1) return res.cc('发布失败')
      res.cc('发布成功', 200)
    })
  },
  list: (req, res) => {
    const sql = `select * from ev_record where is_delete=0`
    db.query(sql, (err, rel) => {
      if (err) return res.cc(err)
      res.cc(rel, 200)
    })
  },
  info: (req, res) => {
    const sql = `select * from ev_record where id=?`
    db.query(sql, req.params.id, (err, rel) => {
      if (err) return res.cc(err)
      res.cc(rel)
    })
  },
  put: (req, res) => {
    const sql = `update  ev_record set ? where id=? `
    const info = {
      ...req.body,
      pub_data: +new Date(),
      author_id: req.user.id,
    }
    db.query(sql, [info, req.body.id], (err, rel) => {
      if (err) return res.cc(err)
      if (rel.affectedRows === 1) return res.cc('修改成功', 200)
      res.cc('修改失败')
    })
  },
  del: (req, res) => {
    const sql = `update ev_record set is_delete=1 where id=?`
    db.query(sql, req.params.id, (err, rel) => {
      if (err) return res.cc(err)
      if (rel.affectedRows === 1) return res.cc('清除成功', 200)
      res.cc('修改失败')
    })
  },
}
